package com.ryg.animatemenu;

import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.Animator.AnimatorListener;
import com.nineoldandroids.animation.AnimatorSet;
import com.nineoldandroids.animation.ObjectAnimator;

public class MainActivity extends Activity {

	private static final int MOVE_OUT = 0X01;
	private static final int SPRING_OUT = 0X02;
	private static final int MOVE_BACK = 0X03;
	private static final int SPRING_BACK = 0X04;

	private int animate;
	private int total;
	private Button mMenuButton;
	private Button mItemButton1;
	private Button mItemButton2;
	private Button mItemButton3;
	private Button mItemButton4;
	private Button mItemButton5;

	private boolean mIsMenuOpen = false;
	private Button[] viewButtons;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initView();

		animate = WindowManageUtil.getWindMannageD(MainActivity.this);
		total = 5;
	}

	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void initView() {
		mMenuButton = (Button) findViewById(R.id.menu);
		mMenuButton.setOnClickListener(new AnimateButtonOnClickListener('A'));

		mItemButton1 = (Button) findViewById(R.id.item1);
		mItemButton1.setOnClickListener(new AnimateButtonOnClickListener('B'));

		mItemButton2 = (Button) findViewById(R.id.item2);
		mItemButton2.setOnClickListener(new AnimateButtonOnClickListener('C'));

		mItemButton3 = (Button) findViewById(R.id.item3);
		mItemButton3.setOnClickListener(new AnimateButtonOnClickListener('D'));

		mItemButton4 = (Button) findViewById(R.id.item4);
		mItemButton4.setOnClickListener(new AnimateButtonOnClickListener('E'));

		mItemButton5 = (Button) findViewById(R.id.item5);
		mItemButton5.setOnClickListener(new AnimateButtonOnClickListener('F'));
		

		viewButtons = new Button[] { mItemButton1, mItemButton2, mItemButton3, mItemButton4, mItemButton5};
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		mMenuButton.performClick();
		getMenuInflater().inflate(R.menu.main, menu);
		return false;
	}

	class AnimateButtonOnClickListener implements OnClickListener {

		char type;

		public AnimateButtonOnClickListener(char type) {
			this.type = type;
		}

		@Override
		public void onClick(View v) {
			switch (type) {
			case 'A':
				if (!mIsMenuOpen) {
					mIsMenuOpen = true;
					AnimateButton();
					for (int i = 0; i <= total - 1; i++) {
						doAnimateOpen(viewButtons[i], i, total, animate, MOVE_OUT);
					}
				} else {
					mIsMenuOpen = false;
					AnimateButton();
					for (int i = 0; i <= total - 1; i++) {
						doAnimateClose(viewButtons[i], i, total, animate);
					}
				}
				break;

			case 'B':
				Toast.makeText(MainActivity.this, "你点击了1" + v.toString(), Toast.LENGTH_SHORT).show();
				break;

			case 'C':
				Toast.makeText(MainActivity.this, "你点击了2" + v.toString(), Toast.LENGTH_SHORT).show();
				break;

			case 'D':
				Toast.makeText(MainActivity.this, "你点击了3" + v.toString(), Toast.LENGTH_SHORT).show();
				break;

			case 'E':
				Toast.makeText(MainActivity.this, "你点击了4" + v.toString(), Toast.LENGTH_SHORT).show();
				break;

			case 'F':
				Toast.makeText(MainActivity.this, "你点击了5" + v.toString(), Toast.LENGTH_SHORT).show();
				break;
				
			default:
				break;
			}
		}
	}

	/**
	 * 主Button
	 */
	private void AnimateButton() {
		AnimatorSet set = new AnimatorSet();
		if (mIsMenuOpen) {
			set.playTogether(ObjectAnimator.ofFloat(mMenuButton, "rotation", 0, -135));
		} else {
			set.playTogether(ObjectAnimator.ofFloat(mMenuButton, "rotation", -135, 0));
		}
		set.setDuration(200).start();
	}

	/**
	 * 打开菜单的动画
	 * 
	 * @param view
	 *            执行动画的view
	 * @param index
	 *            view在动画序列中的顺序
	 * @param total
	 *            动画序列的个数
	 * @param radius
	 *            动画半径
	 * @param type
	 *            MOVE_OUT 正常移动 ，SPRING_OUT 弹簧移动，SPRING_BACK 弹簧收回
	 */
	private void doAnimateOpen(final View view, final int index, final int total, final int radius, int type) {
		int springTranslation = 10;
		if (view.getVisibility() != View.VISIBLE) {
			view.setVisibility(View.VISIBLE);
		}
		final double degree = -Math.PI * (total - index) / (total + 1);
		final int translationX = (int) (radius * Math.cos(degree));
		final int translationY = (int) (radius * Math.sin(degree));
		AnimatorSet set = new AnimatorSet();
		if (MOVE_OUT == type) {
			// 包含平移、缩放和透明度动画
			set.playTogether(ObjectAnimator.ofFloat(view, "rotation", 0, 330),//
					ObjectAnimator.ofFloat(view, "translationX", 0, translationX + (int) (springTranslation * Math.cos(degree))),//
					ObjectAnimator.ofFloat(view, "translationY", 0, translationY + (int) (springTranslation * Math.sin(degree))), //
					ObjectAnimator.ofFloat(view, "alpha", 0f, 1f));
			set.setDuration(300).start();
			set.addListener(new AnimatorListener() {
				@Override
				public void onAnimationStart(Animator arg0) {
				}

				@Override
				public void onAnimationRepeat(Animator arg0) {
				}

				@Override
				public void onAnimationEnd(Animator arg0) {
					doAnimateOpen(view, index, total, radius, SPRING_OUT);
				}

				@Override
				public void onAnimationCancel(Animator arg0) {
				}
			});
		} else if (SPRING_OUT == type) {
			// 包含平移、缩放和透明度动画
			set.playTogether(ObjectAnimator.ofFloat(view, "rotation", 330, 350),
					ObjectAnimator.ofFloat(view, "translationX", translationX + (int) (springTranslation * Math.cos(degree)), translationX - (int) (springTranslation * Math.cos(degree))),
					ObjectAnimator.ofFloat(view, "translationY", translationY + (int) (springTranslation * Math.sin(degree)), translationY - (int) (springTranslation * Math.sin(degree))));
			set.setDuration(50).start();
			set.addListener(new AnimatorListener() {
				@Override
				public void onAnimationStart(Animator arg0) {
				}

				@Override
				public void onAnimationRepeat(Animator arg0) {
				}

				@Override
				public void onAnimationEnd(Animator arg0) {
					doAnimateOpen(view, index, total, radius, SPRING_BACK);
				}

				@Override
				public void onAnimationCancel(Animator arg0) {
				}
			});
		} else if (SPRING_BACK == type) {
			// 包含平移、缩放和透明度动画
			set.playTogether(ObjectAnimator.ofFloat(view, "rotation", 350, 360),
					ObjectAnimator.ofFloat(view, "translationX", translationX - (int) (springTranslation * Math.cos(degree)), translationX),
					ObjectAnimator.ofFloat(view, "translationY", translationY - (int) (springTranslation * Math.sin(degree)), translationY));
			// 动画周期为500ms
			set.setDuration(50).start();
		}
	}

	/**
	 * 关闭菜单的动画
	 * 
	 * @param view
	 *            执行动画的view
	 * @param index
	 *            view在动画序列中的顺序
	 * @param total
	 *            动画序列的个数
	 * @param radius
	 *            动画半径
	 */
	private void doAnimateClose(final View view, int index, int total, int radius) {
		if (view.getVisibility() != View.VISIBLE) {
			view.setVisibility(View.VISIBLE);
		}
		final double degree = -Math.PI * (total - index) / (total + 1);
		final int translationX = (int) (radius * Math.cos(degree));
		final int translationY = (int) (radius * Math.sin(degree));
		AnimatorSet set = new AnimatorSet();

		// 包含平移、缩放和透明度动画
		set.playTogether(ObjectAnimator.ofFloat(view, "rotation", 0, -720), //
				ObjectAnimator.ofFloat(view, "translationX", translationX, translationX + (int) (5 * Math.cos(degree))),//
				ObjectAnimator.ofFloat(view, "translationY", translationY, translationY + (int) (5 * Math.sin(degree)))//
		// ObjectAnimator.ofFloat(view, "scaleX", 1f, 0f),
		// ObjectAnimator.ofFloat(view, "scaleY", 1f, 0f),
		// ObjectAnimator.ofFloat(view, "alpha", 1f, 1f)
		);
		// 为动画加上事件监听，当动画结束的时候，我们把当前view隐藏
		set.addListener(new AnimatorListener() {
			@Override
			public void onAnimationStart(Animator animator) {
			}

			@Override
			public void onAnimationRepeat(Animator animator) {
			}

			@Override
			public void onAnimationEnd(Animator animator) {
				AnimatorSet set = new AnimatorSet();
				// 包含平移、缩放和透明度动画
				set.playTogether(ObjectAnimator.ofFloat(view, "rotation", 0, -1400), //
						ObjectAnimator.ofFloat(view, "translationX", translationX + (int) (5 * Math.cos(degree)), 0),//
						ObjectAnimator.ofFloat(view, "translationY", translationY + (int) (5 * Math.sin(degree)), 0));
				// 为动画加上事件监听，当动画结束的时候，我们把当前view隐藏
				set.addListener(new AnimatorListener() {
					@Override
					public void onAnimationStart(Animator animator) {
					}

					@Override
					public void onAnimationRepeat(Animator animator) {
					}

					@Override
					public void onAnimationEnd(Animator animator) {
						view.setVisibility(View.GONE);
					}

					@Override
					public void onAnimationCancel(Animator animator) {

					}
				});
				set.setDuration(1 * 170).start();
			}

			@Override
			public void onAnimationCancel(Animator animator) {

			}
		});

		set.setDuration(1 * 230).start();
	}
}
